﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task2_Fibonacci
{
    class Task2_Fibonacci
    {
        static void Main(string[] args)
        {
            int number = GetNumberFromInput();

            PrintFibonacciNumbersFrom(number);
        }

        private static int GetNumberFromInput()
        {
            int number;
            Console.Write("Please, enter a number: ");
            string inputLine = Console.ReadLine();
            bool parseStringResult = int.TryParse(inputLine, out number);
            while (!parseStringResult || number < 1)
            {
                Console.Write("Incorrect input! Please, enter a number: ");
                inputLine = Console.ReadLine();
                parseStringResult = int.TryParse(inputLine, out number);
            }
            return number;
        }

        private static void PrintFibonacciNumbersFrom(int number)
        {
            if (number == null)
            {
                throw new NullReferenceException();
            }
            if (number < 1)
            {
                throw new ArgumentOutOfRangeException();
            }

            if (number < 1)
            {
                Console.WriteLine("Incorrect input!");
            }
            if (number == 1)
            {
                Console.WriteLine(0);
            }
            else if (number == 2)
            {
                Console.WriteLine("0, 1");
            }
            else
            {
                Console.Write("0, 1");
                int currentFirstElement = 0;
                int currentSecondElement = 1;
                int currentInSuccession = 2;
                while (currentInSuccession < number)
                {
                    int newFibonacciElement = currentFirstElement + currentSecondElement;
                    Console.Write(", {0}", newFibonacciElement);
                    currentFirstElement = currentSecondElement;
                    currentSecondElement = newFibonacciElement;
                    currentInSuccession++;
                }
                Console.WriteLine();
            }
        }
    }
}
